/*******************************************************************************
* Who Becomes a Teacher? Experimental Evidence from an Information Intervention

* Purpose: Create tables/figures for the appendix of the paper.

* Created by: Antonella Rivera
* Created on: 21/09/2024
* Last modified on: 
* Last modified by: 
* Edits history:

*******************************************************************************/

********************************************************************************
* Set up
********************************************************************************
clear all
set more off
capture log close

if "`c(username)'"=="ANTONELLAR"  {
	global directory = "C:/Users/ANTONELLAR/Dropbox/TeachersPeru/7. Text/International Journal of Educational Development/replication_package/analysis" 
} 

global inputs = "$directory/inputs"
global results "$directory/outputs"
global temp "$directory/temp"

********************************************************************************
* Appendix Table 1: Balance Table without Replacements
********************************************************************************
use "$inputs/experiment_data.dta", replace

drop if compliant == 3

local sample_desc "age female ses failed_class gpa held_back Pmath_std_c Preading_std_c Piq_std_c Pvocab_std_c Pextroversion_std_c Pagreeableness_std_c Pconscient_std_c Pneuro_std_c Popen_std_c Pempat_std_c Ppatien_std_c Pavers_std_c Pfirme_std_c int_teach pub univ s_costs_std_i s_job_quality_std_i s_interests_std_i s_society_std_i knows_changes changes_positive gov_changes"
local sample_desc2 "age female ses failed_class gpa held_back Pmath_std_i Preading_std_i Piq_std_i Pvocab_std_i Pextroversion_std_i Pagreeableness_std_i Pconscient_std_i Pneuro_std_i Popen_std_i Pempat_std_i Ppatien_std_i Pavers_std_i Pfirme_std_i int_teach pub univ s_costs_std_i s_job_quality_std_i s_interests_std_i s_society_std_i knows_changes changes_positive gov_changes"
local education	"patience vocation family easy scholarships prestige wages"

mat define tmat = J(37, 6, .)

* Columns 1 to 3 (Control, Treatment, p-value)
local i = 1

foreach var in `sample_desc' {

	local lab`i' = "`var'"
	local rnum = `i'

	qui areg `lab`i'' treatment school_admin, a(strata) cluster(cod_mod)

	local d1 = _b[_cons] + _b[treatment]
	local d0 = _b[_cons]
	matrix tmat[`rnum', 1] = `d0'
	matrix tmat[`rnum', 2] = `d1'	
	matrix tmat[`rnum', 3] = r(table)[4,1]

	local i = `i' + 1

}

* Columns 4 to 6 (Intention to teach)
local i = 1
foreach var in `sample_desc2' `education'{
	
	local lab`i' = "`var'"
	local rnum = `i'
	
	qui areg `lab`i'' int_teach school_admin, a(strata) cluster(cod_mod)
	
	local d1 = _b[_cons] + _b[int_teach]
	local d0 = _b[_cons]
	matrix tmat[`rnum', 4] = `d0'
	matrix tmat[`rnum', 5] = `d1'
	matrix tmat[`rnum', 6] = r(table)[4,1]
	
	local i = `i' + 1
	
}

* Observations
qui su treatment if treatment == 1
local NT = r(N)
qui su treatment if treatment == 0
local NC = r(N)
qui su int_teach if int_teach == 1
local YI = r(N)
qui su int_teach if int_teach == 0
local NI = r(N)

matrix tmat[37,1] = `NC'
matrix tmat[37,2] = `NT'
matrix tmat[37,4] = `NI'
matrix tmat[37,5] = `YI'

* Format table
********************************************************************************
global names "`sample_desc' `education'"

matrix rownames tmat = $names N
matrix colnames tmat = Control Treatment "p-value" Yes No "p-value"
mat list tmat, format(%20.2fc)

svmat tmat 
forvalues i = 1(1)6 {
  gen var`i' = string(tmat`i',"%20.2fc")
}
drop tmat*

gen Variable = ""
local i = 1                              
foreach var in $names {
	local lab: variable label `var'  
	replace Variable = "`lab'" in `i'
	local i = `i' + 1
}
drop if _n >= 38
 
ren var1 Control
ren var2 Treatment
ren var3 pvalue
ren var4 Yes
ren var5 No
ren var6 Pvalue
label var Variable ""
label var No "No Intent to teach"
label var Yes "Intent to teach"
label var Pvalue "P-value of equality"
keep Variable Control Treatment pvalue Yes No Pvalue
order Variable Control Treatment pvalue Yes No Pvalue

replace Variable = "\midrule Observations" in 37
replace Yes = "." if Variable == "Intention to Teach (%)"
replace No = "." if Variable == "Intention to Teach (%)"
replace Pvalue = "." if Variable == "Intention to Teach (%)"
replace Pvalue = " " if Variable == "\midrule Observations" 
replace pvalue = " " if Variable == "\midrule Observations" 

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 3, before(1)
replace latex = "\toprule & & & & \multicolumn{3}{c}{Intention to teach} \\" in 1
replace latex = "& Control & Treatment & p-value & No & Yes & p-value \\" in 2
replace latex = "\midrule \multicolumn{7}{c}{A. Socio-demographic Characteristics} \\ \midrule" in 3

insobs 2, before(10)
replace latex = "\midrule \multicolumn{7}{c}{B. Standardized Test Scores} \\" in 10
replace latex = "\midrule \multicolumn{7}{c}{Cognitive} \\" in 11

insobs 1, before(16)
replace latex = "\multicolumn{7}{c}{Non-Cognitive} \\" in 16

insobs 1, before(26)
replace latex = "\midrule \multicolumn{7}{c}{C. Career Choice} \\ \midrule" in 26

insobs 1, before(34)
replace latex = "\midrule \multicolumn{7}{c}{D. Changes in Teaching Career} \\ \midrule" in 34

insobs 1, before(38)
replace latex = "\midrule \multicolumn{7}{c}{E. Reasons to Study (or not) Education} \\ \midrule" in 38

insobs 2, after(46)
replace latex = "\bottomrule & & & & & & \\" in 47
replace latex = "\end{tabular}" in 48

insobs 1, before(1)
replace latex = "\begin{tabular}{lcccccc}" in 1

cap file close myfile
file open myfile using "$results/Tables/balance_table_wo_replacements.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

**************************************************************************************************************************
* Appendix Table 2: Intention to Teach: Student Characteristics of those who Intended to Enroll in Tertiary Education
**************************************************************************************************************************
use "$inputs/experiment_data.dta", replace

* Variables
local sample_desc2 "age female ses failed_class gpa held_back Pmath_std_i Preading_std_i Piq_std_i Pvocab_std_i Pextroversion_std_i Pagreeableness_std_i Pconscient_std_i Pneuro_std_i Popen_std_i Pempat_std_i Ppatien_std_i Pavers_std_i Pfirme_std_i"

* Estimation of statistics 
mat define tmat = J(20, 3, .)
local i = 1
foreach var in `sample_desc2' {
	local lab`i' = "`var'"
	local rnum = `i'
	qui areg `lab`i'' int_teach school_admin if int_enroll == 1, a(strata) cluster(cod_mod)
	local d1 = _b[_cons] + _b[int_teach]
	local d0 = _b[_cons]
	matrix tmat[`rnum', 1] = `d0'
	matrix tmat[`rnum', 2] = `d1'
	matrix tmat[`rnum', 3] = r(table)[4,1]
	local i = `i' + 1
}

* Compile matrix
qui su int_teach if int_teach == 1 & int_enroll == 1
local NT = r(N)
qui su int_teach if int_teach == 0 & int_enroll == 1
local NC = r(N)

matrix tmat[20,1] = `NC'
matrix tmat[20,2] = `NT'

global names "`sample_desc2'"

matrix rownames tmat =  `sample_desc2' N
matrix colnames tmat = "No Intent" "Intent to Teach" "P-Value"
mat list tmat, format(%20.2fc)

* Export to tex
svmat tmat 
drop if tmat1 == .
forvalues i = 1(1)3 {
	gen var`i' = string(tmat`i',"%20.2fc")
}
drop tmat*

* Extract variable names from variable labels
gen Variable = ""
local i = 1                              
foreach var in $names {
	local lab: variable label `var'  
	replace Variable = "`lab'" in `i'
	local i = `i'+ 1
}

* Organize table columns
ren var1 No
ren var2 Yes
ren var3 Pvalue
label var Variable ""
label var No "No Intent to teach"
label var Yes "Intent to teach"
label var Pvalue "P-value of equality"
keep Variable No Yes Pvalue
order Variable No Yes Pvalue

replace Variable = "\midrule Observations" in 20
replace Pvalue = " " if Variable == "\midrule Observations" 

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 3, before(1)
replace latex = "\toprule & \multicolumn{2}{c}{Intention to teach} & \\" in 1
replace latex = "& No & Yes & p-value \\" in 2
replace latex = "\midrule \multicolumn{4}{c}{A. Socio-demographic Characteristics} \\ \midrule" in 3

insobs 2, before(10)
replace latex = "\midrule \multicolumn{4}{c}{B. Standardized Test Scores} \\" in 10
replace latex = "\midrule \multicolumn{4}{c}{Cognitive} \\" in 11

insobs 1, before(16)
replace latex = "\multicolumn{4}{c}{Non-Cognitive} \\" in 16

insobs 2, after(26)
replace latex = "\bottomrule & & & \\" in 27
replace latex = "\end{tabular}" in 28

insobs 1, before(1)
replace latex = "\begin{tabular}{lccc}" in 1

cap file close myfile
file open myfile using "$results/Tables/balance_itt_ite.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

********************************************************************************
* Appendix Table 3: Attrition
********************************************************************************
use "$inputs/experiment_data.dta", replace

gen attrited = (endline == 0)

* Data prep
global reg1 treatment
global reg2 treatment age Pmath Preading Pvocab Ppatien
global reg3 treatment age Pmath Preading Pvocab Ppatien ses female
global reg4 treatment age Pmath Preading Pvocab Ppatien ses female teaching_career_before

global regb1 
global regb2 mage mPmath mPreading mPvocab mPpatien
global regb3 mage mPmath mPreading mPvocab mPpatien mses mfemale
global regb4 mage mPmath mPreading mPvocab mPpatien mses mfemale mteaching_career_before

qui su attrited 
local NA = r(N)
su attrited if treatment == 0
local mu0 = round(r(mean),.001)

forvalues i = 1(1)4 {
	if `i' == 1 local append = "replace"
	if `i' > 1  local append = "append"
	qui reg attrited ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
	outreg2 using "$temp/attrition.dta", `append' keep(treatment) dec(3) symbol(***, **, *) nocon nonot nor dta addtext(Mean, `mu0')

}	

use "$temp/attrition_dta.dta", clear
forvalues j = 7(1)9 { 
	gen v`j' = ""
}

replace v7 = v2[7] in 4
replace v8 = v2[8] in 4

drop if _n < 4 
drop if _n >= 4
drop v1
order v9

replace v8 = "0" + v8 if v8 != ""

replace v9 = "\midrule Attrited" in 1

insobs 3, after(3)

replace v9 = "\midrule Main Control Set" in 4
replace v2 = "No" in 4
replace v3 = "Yes" in 4
replace v4 = "Yes" in 4
replace v5 = "Yes" in 4

replace v9 = "Socio-Demographic Variables" in 5
replace v2 = "No" in 5
replace v3 = "No" in 5
replace v4 = "Yes" in 5
replace v5 = "Yes" in 5

replace v9 = "Opinion of Teaching Career" in 6
replace v2 = "No" in 6
replace v3 = "No" in 6
replace v4 = "No" in 6
replace v5 = "Yes" in 6

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, after(6)

replace latex = "\bottomrule & & & & & & \\" in 7
replace latex = "\end{tabular}" in 8

insobs 3, before(1)
replace latex = "\begin{tabular}{lcccccc}" in 1
replace latex = "\toprule & (1) & (2) & (3) & (4) & Obs. & Control \\" in 2
replace latex = "& & & & & & Mean \\" in 3

cap file close myfile
file open myfile using "$results/Tables/attrition.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

********************************************************************************
* Appendix Table 4: Sample Description between respondents and non-respondents
********************************************************************************
* Estimations of statistics
********************************************************************************
use "$inputs/experiment_data.dta", replace

gen respondent = (endline == 1)

local sample_desc "age female ses failed_class gpa held_back Pmath_std_c Preading_std_c Piq_std_c Pvocab_std_c Pextroversion_std_c Pagreeableness_std_c Pconscient_std_c Pneuro_std_c Popen_std_c Pempat_std_c Ppatien_std_c Pavers_std_c Pfirme_std_c int_teach pub univ s_costs_std_i s_job_quality_std_i s_interests_std_i s_society_std_i knows_changes changes_positive gov_changes"

mat define tmat = J(37, 3, .)

* Columns 1 to 3 (Control, Treatment, p-value)
local i = 1

foreach var in `sample_desc' {

	local lab`i' = "`var'"
	local rnum = `i'
	
	qui sum `lab`i'' if respondent == 0    // Non-respondent mean
    local d0 = r(mean)
	qui sum `lab`i'' if respondent == 1    // Respondent mean
    local d1 = r(mean)
	
	matrix tmat[`rnum', 1] = `d0'
	matrix tmat[`rnum', 2] = `d1'	

	qui areg `lab`i'' respondent school_admin, a(strata) cluster(cod_mod)

	matrix tmat[`rnum', 3] = r(table)[4,1]

	local i = `i' + 1

}

* Observations
qui su respondent if respondent == 0
local NNR = r(N)
qui su respondent if respondent == 1
local NR = r(N)

matrix tmat[37,1] = `NNR'
matrix tmat[37,2] = `NR'

* Format table
********************************************************************************
global names "`sample_desc'"

matrix rownames tmat = $names N
matrix colnames tmat = Non_Respondents Respondents "p-value" 
mat list tmat, format(%20.2fc)

svmat tmat 
forvalues i = 1(1)3 {
  gen var`i' = string(tmat`i',"%20.2fc")
}
drop tmat*

gen Variable = ""
local i = 1                              
foreach var in $names {
	local lab: variable label `var'  
	replace Variable = "`lab'" in `i'
	local i = `i' + 1
}
drop if var1 == "."
 
ren var1 non_respondents
ren var2 respondents
ren var3 pvalue
keep Variable non_respondents respondents pvalue 
order Variable non_respondents respondents pvalue

replace Variable = "\midrule Observations" in 30
replace pvalue = " " if Variable == "\midrule Observations" 

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\toprule & Non-Respondents & Respondents & p-value \\" in 1
replace latex = "\midrule \multicolumn{4}{c}{A. Socio-demographic Characteristics} \\ \midrule" in 2

insobs 2, before(9)
replace latex = "\midrule \multicolumn{4}{c}{B. Standardized Test Scores} \\" in 9
replace latex = "\midrule \multicolumn{4}{c}{Cognitive} \\" in 10

insobs 1, before(15)
replace latex = "\multicolumn{4}{c}{Non-Cognitive} \\" in 15

insobs 1, before(25)
replace latex = "\midrule \multicolumn{4}{c}{C. Career Choice} \\ \midrule" in 25

insobs 1, before(33)
replace latex = "\midrule \multicolumn{4}{c}{D. Changes in Teaching Career} \\ \midrule" in 33

insobs 2, after(37)
replace latex = "\bottomrule & & & \\" in 38
replace latex = "\end{tabular}" in 39

insobs 1, before(1)
replace latex = "\begin{tabular}{lccc}" in 1

cap file close myfile
file open myfile using "$results/Tables/balance_attrition.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

********************************************************************************
* Appendix Table 5: Students' Information Retention After Treatment
********************************************************************************
use "$inputs/experiment_data.dta", replace

keep if treatment == 1

* Compile matrix
mat define cambios = J(6,2,.)
	
forval q = 1(1)6 {
	forval i = 1(1)2 {

		tab q1_`q' if q1_`q' == `i'
		local a`i'`q' = r(N)
		
		mat cambios[`q',`i'] = `a`i'`q''
	}
}
mat colnames cambios = Yes No
mat list cambios

* Get original row names of matrix (and row count)
local rownames : rowfullnames cambios
local c : word count `rownames'

* Get original column names of matrix and substitute out _cons
local names : colfullnames cambios

* Rename columns of matrix
matrix colnames cambios = `newnames'

* Convert to dataset
clear
svmat cambios

* Add matrix row names to dataset
gen rownames = ""
forvalues i = 1/`c' {
    replace rownames = "`:word `i' of `rownames''" in `i'
}
order rownames cambios*
egen total = rowtotal(cambios1 cambios2)
gen var1 = int(100*(cambios1/total))
gen var2 = int(100*(cambios2/total))
drop cambios* total

* Organize table columns
ren var1 Yes
ren var2 No
ren rownames Question
replace Question = "\midrule 1. More scholarships to study education" if _n==1
replace Question = "2. Reduced work hours" if _n==2
replace Question = "3. Performance evaluations for promotions" if _n==3
replace Question = "4. Higher wages for promotions" if _n==4
replace Question = "5. Notable teachers can become congresspersons or president" if _n==5
replace Question = "6. More scholarships for teacher training" if _n==6

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, before(1)
replace latex = "\begin{tabular}{lcc}" in 1
replace latex = "\toprule Which changes do you remember? & Yes & No \\" in 2

insobs 2, after(8)
replace latex = "\bottomrule & & \\" in 9
replace latex = "\end{tabular}" in 10

cap file close myfile
file open myfile using "$results/Tables/retention.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

********************************************************************************
* Appendix Table 6: Changes in Positive Opintions about the Teaching Career
********************************************************************************
use "$inputs/experiment_data.dta", replace

keep if treatment == 1 

mat define coefs = J(4, 3, .)

areg teaching_career_before Pmath ses female Ppatien ///
mPmath mses mfemale mPpatien school_admin if change_opinion != ., ///
a(strata) cluster(cod_mod)

** Math
matrix coefs[1, 1] = r(table)[1, 1]
matrix coefs[1, 2] = r(table)[2, 1]
matrix coefs[1, 3] = r(table)[4, 1]
** Socio-economic Level
matrix coefs[2, 1] = r(table)[1, 2]
matrix coefs[2, 2] = r(table)[2, 2]
matrix coefs[2, 3] = r(table)[4, 2]
** Female 
matrix coefs[3, 1] = r(table)[1, 3]
matrix coefs[3, 2] = r(table)[2, 3]
matrix coefs[3, 3] = r(table)[4, 3]
** Patience
matrix coefs[4, 1] = r(table)[1, 4]
matrix coefs[4, 2] = r(table)[2, 4]
matrix coefs[4, 3] = r(table)[4, 4]

clear
svmat coefs

gen n = _n
rename coefs1 b
rename coefs2 se
rename coefs3 pvalue
gen S = "*" if pvalue < 0.1
replace S = "**" if pvalue < 0.05
replace S = "***" if pvalue < 0.01

preserve
gen OLS = string(b, "%20.3fc")
replace OLS = OLS + S
gen order_no = 1
keep n order_no OLS
tempfile means
save `means', replace
restore

keep se n 
gen OLS = string(se, "%20.3fc")	
replace OLS = "(" + OLS + ")"
drop se
gen order_no = 2

append using `means'	
sort n order_no
order n order_no

tempfile col1
save `col1', replace

* Change in Opinion after the treatment
use "$inputs/experiment_data.dta", replace

keep if treatment == 1 

mat define coefs = J(4, 3, .)

areg change_opinion Pmath ses female Ppatien ///
mPmath mses mfemale mPpatien school_admin, ///
a(strata) cluster(cod_mod)

** Math
matrix coefs[1, 1] = r(table)[1, 1]
matrix coefs[1, 2] = r(table)[2, 1]
matrix coefs[1, 3] = r(table)[4, 1]
** Socio-economic Level
matrix coefs[2, 1] = r(table)[1, 2]
matrix coefs[2, 2] = r(table)[2, 2]
matrix coefs[2, 3] = r(table)[4, 2]
** Female 
matrix coefs[3, 1] = r(table)[1, 3]
matrix coefs[3, 2] = r(table)[2, 3]
matrix coefs[3, 3] = r(table)[4, 3]
** Patience
matrix coefs[4, 1] = r(table)[1, 4]
matrix coefs[4, 2] = r(table)[2, 4]
matrix coefs[4, 3] = r(table)[4, 4]

clear
svmat coefs

gen n = _n
rename coefs1 b
rename coefs2 se
rename coefs3 pvalue
gen S = "*" if pvalue < 0.1
replace S = "**" if pvalue < 0.05
replace S = "***" if pvalue < 0.01

preserve
gen OLS2 = string(b, "%20.3fc")
replace OLS2 = OLS2 + S
gen order_no = 1
keep n order_no OLS2
tempfile means
save `means', replace
restore

keep se n 
gen OLS2 = string(se, "%20.3fc")	
replace OLS2 = "(" + OLS2 + ")"
drop se
gen order_no = 2

append using `means'	
sort n order_no
order n order_no

merge 1:1 n order_no using `col1', nogen
sort n order_no
drop n order_no
order OLS OLS2

gen var = ""
replace var = "\midrule Mathematics" in 1
replace var = "Socio-economic Level" in 3
replace var = "Female" in 5
replace var = "Patience" in 7
order var

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 3, before(1)
replace latex = "\toprule & Opinion before & Change in Opinion \\" in 1
replace latex = "& the treatment & after the treatment \\" in 2
replace latex = "& (1) & (2) \\" in 3

insobs 3, after(11)
replace latex = "\midrule Observations & 2,984 & 2,984 \\" in 12
replace latex = "\bottomrule & & \\" in 13
replace latex = "\end{tabular}" in 14

insobs 1, before(1)
replace latex = "\begin{tabular}{lcc}" in 1

cap file close myfile
file open myfile using "$results/Tables/change_opinion.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

********************************************************************************
* Appendix Figure 2: Treatment Effects Conditional to Area of Intended Enrollment
********************************************************************************
use "$inputs/experiment_data.dta", replace

* Define global  
global reg1 treatment
global reg2 treatment age Pmath Preading Pvocab Ppatien
global reg3 treatment age Pmath Preading Pvocab Ppatien ses female
global reg4 treatment age Pmath Preading Pvocab Ppatien ses female teaching_career_before

global regb1 
global regb2 mage mPmath mPreading mPvocab mPpatien
global regb3 mage mPmath mPreading mPvocab mPpatien mses mfemale
global regb4 mage mPmath mPreading mPvocab mPpatien mses mfemale mteaching_career_before

* Probability of Studying Education / Intention to Enroll in Education
mat define coef = J(8,4,.)
forval i = 1(1)4 {
	qui reg education ${reg`i'} ${regb`i'} school_admin i.strata if int_enroll_1 == 1,  vce(cluster cod_mod)
	estimates store A
	
	mat coef[1,1]=_b[treatment]
	mat coef[1,2]=_se[treatment]
	mat coef[1,3]=_b[treatment]/_se[treatment]
	mat coef[1,4]=2*ttail(e(df_r),abs(_b[treatment]/_se[treatment]))
}

* Probability of Studying Education / Intention to Enroll in STEM
forval i = 1(1)4 {
	qui reg education ${reg`i'} ${regb`i'} school_admin i.strata if int_enroll_2 == 1,  vce(cluster cod_mod)
	estimates store E
	
	mat coef[2,1]=_b[treatment]
	mat coef[2,2]=_se[treatment]	
	mat coef[2,3]=_b[treatment]/_se[treatment]
	mat coef[2,4]=2*ttail(e(df_r),abs(_b[treatment]/_se[treatment]))
}

* Probability of Studying Education / Intention to Enroll in Soc. Sciences
forval i = 1(1)4 {
	qui reg education ${reg`i'} ${regb`i'} school_admin i.strata if int_enroll_3 == 1,  vce(cluster cod_mod)
	estimates store D
	
	mat coef[3,1]=_b[treatment]
	mat coef[3,2]=_se[treatment]
	mat coef[3,3]=_b[treatment]/_se[treatment]
	mat coef[3,4]=2*ttail(e(df_r),abs(_b[treatment]/_se[treatment]))
}

* Probability of Studying Education / Intention to Enroll in Administration
forval i = 1(1)4 {
	qui reg education ${reg`i'} ${regb`i'} school_admin i.strata if int_enroll_4 == 1,  vce(cluster cod_mod)
	estimates store F
	
	mat coef[4,1]=_b[treatment]
	mat coef[4,2]=_se[treatment]
	mat coef[4,3]=_b[treatment]/_se[treatment]
	mat coef[4,4]=2*ttail(e(df_r),abs(_b[treatment]/_se[treatment]))
}

* Probability of Studying Education / Intention to Enroll in Arts
forval i = 1(1)4 {
	qui reg education ${reg`i'} ${regb`i'} school_admin i.strata if int_enroll_5 == 1,  vce(cluster cod_mod)
	estimates store G
	
	mat coef[5,1]=_b[treatment]
	mat coef[5,2]=_se[treatment]	
	mat coef[5,3]=_b[treatment]/_se[treatment]
	mat coef[5,4]=2*ttail(e(df_r),abs(_b[treatment]/_se[treatment]))
}

* Probability of Studying Education / Intention to Enroll in Law
forval i = 1(1)4 {
	qui reg education ${reg`i'} ${regb`i'} school_admin i.strata if int_enroll_6 == 1,  vce(cluster cod_mod)
	estimates store B
	
	mat coef[6,1]=_b[treatment]
	mat coef[6,2]=_se[treatment]
	mat coef[6,3]=_b[treatment]/_se[treatment]
	mat coef[6,4]=2*ttail(e(df_r),abs(_b[treatment]/_se[treatment]))
}
	
* Probability of Studying Education / Intention to Enroll in Medical Sciences
forval i = 1(1)4 {
	qui reg education ${reg`i'} ${regb`i'} school_admin i.strata if int_enroll_7 == 1,  vce(cluster cod_mod)
	estimates store C
	
	mat coef[7,1]=_b[treatment]
	mat coef[7,2]=_se[treatment]
	mat coef[7,3]=_b[treatment]/_se[treatment]
	mat coef[7,4]=2*ttail(e(df_r),abs(_b[treatment]/_se[treatment]))
}

* Probability of Studying Education / Intention to Enroll in Other
forval i = 1(1)4 {
	qui reg education ${reg`i'} ${regb`i'} school_admin i.strata if int_enroll_8 == 1,  vce(cluster cod_mod)
	estimates store H
	
	mat coef[8,1]=_b[treatment]
	mat coef[8,2]=_se[treatment]
	mat coef[8,3]=_b[treatment]/_se[treatment]
	mat coef[8,4]=2*ttail(e(df_r),abs(_b[treatment]/_se[treatment]))
}

mat colnames coef = term se t p
mat rownames coef = educ stem socsc admin art law medic other

* Graph
mat coefs=coef[1...,1]'
mat se=coef[1...,2]'
mat ub=coefs+se*1.96
mat lb=coefs-se*1.96
mat ci=lb\ub
mat rownames ci=ll95 ul95

coefplot matrix(coefs), vertical ci(ci) yline(0,lcolor(black) lwidth(thin) lpattern(dash)) ylabel(-0.1(0.05)0.3,labsize(small) nogrid) graphregion(color(white)) bgcolor(white) sort(,descending) ytitle("Treatment Effect") xtitle("Area of Intended Enrollment") coeflabels(educ = "Education" law = "Law" medic = "Medical Sciences" socsc = "Social Sciences" stem = "STEM" admin = "Administration" art = "Arts" other = "Other", wrap(14)  labsize(small)) 

graph export "$results/Figures/treatment_cond_majors.eps", as(eps) preview(off) replace

********************************************************************************
* Appendix Table 7: Treatment Effects on Career Choice Without Replacements
********************************************************************************
use "$inputs/experiment_data.dta", replace

keep if endline == 1
drop if compliant == 3

* Data prep
global reg1 treatment
global reg2 treatment age Pmath Preading Pvocab Ppatien
global reg3 treatment age Pmath Preading Pvocab Ppatien ses female
global reg4 treatment age Pmath Preading Pvocab Ppatien ses female teaching_career_before

global regb1 
global regb2 mage mPmath mPreading mPvocab mPpatien
global regb3 mage mPmath mPreading mPvocab mPpatien mses mfemale
global regb4 mage mPmath mPreading mPvocab mPpatien mses mfemale mteaching_career_before

* Estimation of statistics 
qui su education 
local NE = r(N)
su education if treatment == 0
local mu0 = round(r(mean),0.001)

qui su studied 
local NS = r(N)
su studied if treatment == 0
local mu1 = round(r(mean),0.001)

qui su education if int_enroll == 1 
local NI = r(N)
su education if int_enroll == 1 & treatment == 0
local mu2 = round(r(mean),0.001)

forvalues i = 1(1)4 {
	if `i' == 1 local append = "replace"
	if `i' > 1  local append = "append"
		qui reg education ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
		outreg2 using "$temp/longrun1.dta", `append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu0')
		qui reg studied ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
		outreg2 using "$temp/longrun2.dta",`append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu1')
		qui reg education ${reg`i'} ${regb`i'} school_admin i.strata if int_enroll == 1, vce(cluster cod_mod)
		outreg2 using "$temp/longrun3.dta", `append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu2')
}	

* Shift rows to columns 
capture program drop prepforappend
program define prepforappend
{
	forvalues j = 6(1)8 { 
		gen v`j' = ""
	}
	replace v6 = v2[7] in 4
	replace v7 = v2[8] in 4
	replace v8 = v2[2] in 4
	drop if _n < 4 | _n >= 7
	drop v1
	order v8
	save, replace
}
end

forvalues i= 1(1)3 {
	use "$temp/longrun`i'_dta.dta", clear
	prepforappend
}

use "$temp/longrun1_dta.dta", clear
append using "$temp/longrun2_dta.dta"
append using "$temp/longrun3_dta.dta"

replace v7 = "0" + v7 if v7 != ""

replace v8 = "\midrule \multirow{2}{6cm}{Probability of studying an education-related major}" in 1
replace v8 = "\multirow{2}{6cm}{Probability of enrolling in tertiary education}" in 4
replace v8 = "\multirow{2}{6cm}{Probability of studying education (conditional on tertiary education intent)}" in 7

insobs 3, after(9)

replace v8 = "\midrule Main Control Set" in 10 
replace v2 = "No" in 10 
replace v3 = "Yes" in 10 
replace v4 = "Yes" in 10 
replace v5 = "Yes" in 10 

replace v8 = "Socio-Demographic Variables" in 11 
replace v2 = "No" in 11
replace v3 = "No" in 11
replace v4 = "Yes" in 11 
replace v5 = "Yes" in 11 

replace v8 = "Opinion of Teaching Career" in 12
replace v2 = "No" in 12
replace v3 = "No" in 12
replace v4 = "No" in 12
replace v5 = "Yes" in 12

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, after(12)

replace latex = "\bottomrule & & & & & & \\" in 13
replace latex = "\end{tabular}" in 14

insobs 3, before(1)
replace latex = "\begin{tabular}{lcccccc}" in 1
replace latex = "\toprule & (1) & (2) & (3) & (4) & Obs. & Control \\" in 2
replace latex = "& & & & & & Mean \\" in 3

cap file close myfile
file open myfile using "$results/Tables/treatment_effects_rep.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

********************************************************************************
* Appendix Table 8: Effect on Other Majors
********************************************************************************
use "$inputs/experiment_data.dta", replace

gen agro = major == 1 if major != .
gen stem = major == 2 if major != .
gen sociales = major == 3 if major != .
gen salud = major == 4 if major != .
gen educ = major == 5 if major != .
gen humanidades = major == 6 if major != .
gen ing = major == 7 if major != .
gen otros = major == 8 if major != .

* Data prep
global reg1 treatment
global reg2 treatment age Pmath Preading Pvocab Ppatien
global reg3 treatment age Pmath Preading Pvocab Ppatien ses female
global reg4 treatment age Pmath Preading Pvocab Ppatien ses female teaching_career_before

global regb1 
global regb2 mage mPmath mPreading mPvocab mPpatien
global regb3 mage mPmath mPreading mPvocab mPpatien mses mfemale
global regb4 mage mPmath mPreading mPvocab mPpatien mses mfemale mteaching_career_before

* Estimation of statistics 
su agro if treatment == 0
local mu1 = round(r(mean),.001)

su stem if treatment == 0
local mu2 = round(r(mean),.001)

su sociales if treatment == 0
local mu3 = round(r(mean),.001)

su salud if treatment == 0
local mu4 = round(r(mean),.001)

su educ if treatment == 0
local mu5 = round(r(mean),.001)

su humanidades if treatment == 0
local mu6 = round(r(mean),.001)

su ing if treatment == 0
local mu7 = round(r(mean),.001)

su otros if treatment == 0
local mu8 = round(r(mean),.001)

forvalues i = 1(1)4 {
	if `i' == 1 local append = "replace"
	if `i' > 1  local append = "append"
		qui reg agro ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
		outreg2 using "$temp/longrun1.dta",`append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu1')
		qui reg stem ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
		outreg2 using "$temp/longrun2.dta",`append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu2')		
		qui reg sociales ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
		outreg2 using "$temp/longrun3.dta",`append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu3')
		qui reg salud ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
		outreg2 using "$temp/longrun4.dta",`append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu4')	
		qui reg educ ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
		outreg2 using "$temp/longrun5.dta",`append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu5')		
		qui reg humanidades ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
		outreg2 using "$temp/longrun6.dta",`append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu6')
		qui reg ing ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
		outreg2 using "$temp/longrun7.dta",`append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu7')
		qui reg otros ${reg`i'} ${regb`i'} school_admin i.strata, vce(cluster cod_mod)
		outreg2 using "$temp/longrun8.dta",`append' keep(treatment) dec(3) nocon nonot nor dta addtext(Mean, `mu8')
}	

* Shift rows to columns 
capture program drop prepforappend
program define prepforappend
{
	forvalues j = 6(1)8 { 
		gen v`j' = ""
	}
	replace v6 = v2[7] in 4
	replace v7 = v2[8] in 4
	replace v8 = v2[2] in 4
	drop if _n < 4 | _n >= 7
	drop v1
	order v8
	save, replace
}
end

forvalues i= 1(1)8 {
	use "$temp/longrun`i'_dta.dta", clear
	prepforappend
}

use "$temp/longrun1_dta.dta", clear
append using "$temp/longrun2_dta.dta"
append using "$temp/longrun3_dta.dta"
append using "$temp/longrun4_dta.dta"
append using "$temp/longrun5_dta.dta"
append using "$temp/longrun6_dta.dta"
append using "$temp/longrun7_dta.dta"
append using "$temp/longrun8_dta.dta"

destring v7, replace
gen v72 = string(v7, "%20.2fc")
replace v72 = "" if v72 == "."
drop v7
rename v72 v7

replace v8 = "\midrule \multirow{2}{6cm}{Agricultural, Livestock, and Veterinarian}" in 1
replace v8 = "\multirow{2}{6cm}{Natural, Exact, and Computation Sciences}" in 4
replace v8 = "\multirow{2}{6cm}{Law, Social, and Commercial Sciences}" in 7
replace v8 = "\multirow{2}{6cm}{Health Sciences}" in 10
replace v8 = "\multirow{2}{6cm}{Education}" in 13
replace v8 = "\multirow{2}{6cm}{Arts and Humanities}" in 16
replace v8 = "\multirow{2}{6cm}{Engineering, Industry, and Construction}" in 19
replace v8 = "\multirow{2}{6cm}{Other}" in 22

insobs 3, after(24)

replace v8 = "\midrule Main Control Set" in 25 
replace v2 = "No" in 25 
replace v3 = "Yes" in 25 
replace v4 = "Yes" in 25 
replace v5 = "Yes" in 25 

replace v8 = "Socio-Demographic Variables" in 26
replace v2 = "No" in 26
replace v3 = "No" in 26
replace v4 = "Yes" in 26
replace v5 = "Yes" in 26

replace v8 = "Opinion of Teaching Career" in 27
replace v2 = "No" in 27
replace v3 = "No" in 27
replace v4 = "No" in 27
replace v5 = "Yes" in 27

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 2, after(27)

replace latex = "\bottomrule & & & & & & \\" in 28
replace latex = "\end{tabular}" in 29

insobs 3, before(1)
replace latex = "\begin{tabular}{lcccccc}" in 1
replace latex = "\toprule & (1) & (2) & (3) & (4) & Obs. & Control \\" in 2
replace latex = "& & & & & & Mean \\" in 3

cap file close myfile
file open myfile using "$results/Tables/effect_other_majors.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

********************************************************************************
* Appendix Table B.1: Sample Representativity
********************************************************************************
global vars enr_total_5 enr_female_5 age_total_5 turno1 turno2 turno3 ///
public labs libraries gyms enr_total_sec foreign_teaching ///
educ_software computers teachers_female teachers_pedagogy avglevel_magis ///
avghours_teachers

foreach if in "if lima_met == 1" "if universe_lima == 1" "if sample == 1" { 

	if "`if'" == "if lima_met == 1" local cond = "universe"	
	if "`if'" == "if universe_lima == 1" local cond = "eligible"	
	if "`if'" == "if sample == 1" local cond = "sample"	

	local contador = 0

	foreach x in $vars {
		
		local contador = `contador' + 1

		use "$inputs/censo_2014_F0.dta", replace

		keep `if'
		
		collapse (mean) mean_`cond' = `x' 
		
		gen variable = "`x'"
		gen order = `contador'
		
		if `contador' == 1 {
			tempfile descriptive_stats_`cond'
			save `descriptive_stats_`cond'', replace
		}
		
		else {
			append using `descriptive_stats_`cond''
			save `descriptive_stats_`cond'', replace	
		}
	}
}

use `descriptive_stats_universe'
merge 1:1 variable order using `descriptive_stats_eligible', nogen
merge 1:1 variable order using `descriptive_stats_sample', nogen

local vars mean_universe mean_eligible mean_sample

foreach var of local vars {
    gen `var'_s = string(`var', "%20.2fc") 
	drop `var'
	rename `var'_s `var'
}

sort order 

gen rownames = ""
replace rownames = "\midrule Total Students in Senior Year" if _n==1
replace rownames = "Total Female Students in Senior Year" if _n==2
replace rownames = "Average Age of Senior Year Students" if _n==3
replace rownames = "Morning Shift (1 = Yes, 0 = No)" if _n==4
replace rownames = "Afternoon Shift (1 = Yes, 0 = No)" if _n==5
replace rownames = "Full-Day School (1 = Yes, 0 = No)" if _n==6
replace rownames = "Public School (1 = Yes, 0 = No)" if _n==7
replace rownames = "Total Number of Labs" if _n==8
replace rownames = "Total Number of Libraries" if _n==9
replace rownames = "Total Number of Gyms" if _n==10
replace rownames = "Total Secondary Enrollment" if _n==11
replace rownames = "Teaches a Foreign Language (1 = Yes, 0 = No)" if _n==12
replace rownames = "Use of Educational Software (1 = Yes, 0 = No)" if _n==13
replace rownames = "Share of Computers per Student" if _n==14
replace rownames = "Share of Female Teachers" if _n==15
replace rownames = "Average Number of Teachers w$/$ Pedagogy Degree" if _n==16
replace rownames = "Average Magisterial Career Level of Teachers" if _n==17
replace rownames = "Average Teaching Hours of Teachers" if _n==18

drop order variable
order rownames

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 3, before(1)
replace latex = "\begin{tabular}{lccc}" in 1
replace latex = "\toprule Variables & Universe & Eligible & Experimental \\" in 2
replace latex = "& (1) & (2) & (3) \\" in 3

insobs 3, after(21)
replace latex = "\midrule Number of Schools & 2,716 & 624 & 250 \\" in 22
replace latex = "\bottomrule & & & \\" in 23
replace latex = "\end{tabular}" in 24

cap file close myfile
file open myfile using "$results/Tables/sample_representativity_2.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

********************************************************************************
* Appendix Table B.3: Comparison of School Characteristics of Different Groups
********************************************************************************
use "$inputs/censo_2014_F0.dta", replace

global vars enr_total_5 enr_female_5 age_total_5 turno1 turno2 turno3 ///
public labs libraries gyms enr_total_sec foreign_teaching ///
educ_software computers teachers_female teachers_pedagogy avglevel_magis ///
avghours_teachers

mat define pvalues = J(20, 6, .)

local i = 1
foreach x in $vars {
	*80 vs 120
	qui areg `x' treatment if sample == 1 & compliant != 3, a(strata_d) r
	mat r1 = r(table)
	mat pvalues[`i', 1] = r1[4, 1]
	mat pvalues[19, 1] = 80
	mat pvalues[20, 1] = 120
	
	*81 vs 95
	qui areg `x' treatment if sample == 1 & compliant != 2, a(strata_d) r
	mat r1 = r(table)
	mat pvalues[`i', 2] = r1[4, 1]
	mat pvalues[19, 2] = 81
	mat pvalues[20, 2] = 95
	
	*61 vs 65
	qui areg `x' treatment if sample == 1 & compliant == 1, a(strata_d) r
	mat r1 = r(table)
	mat pvalues[`i', 3] = r1[4, 1]
	mat pvalues[19, 3] = 61
	mat pvalues[20, 3] = 65
	
	*176 vs. 74
	cap drop non_compliant
	gen non_compliant = compliant == 2 if sample == 1
	qui areg `x' non_compliant if sample == 1, a(strata_d) r
	mat r1 = r(table)
	mat pvalues[`i', 4] = r1[4, 1]
	mat pvalues[19, 4] = 176
	mat pvalues[20, 4] = 74
	
	*126 vs. 74
	qui areg `x' non_compliant if sample == 1 & compliant != 3, a(strata_d) r
	mat r1 = r(table)
	mat pvalues[`i', 5] = r1[4, 1]
	mat pvalues[19, 5] = 126
	mat pvalues[20, 5] = 74

	*50 vs. 74
	cap drop replacements
	gen replacements = compliant == 3 if sample == 1
	qui areg `x' replacements if sample == 1 & compliant != 1, a(strata_d) r
	mat r1 = r(table)
	mat pvalues[`i', 6] = r1[4, 1]
	mat pvalues[19, 6] = 50
	mat pvalues[20, 6] = 74
	
	local i = `i' + 1
}

clear
svmat pvalues

rename pvalues1 T_C
rename pvalues2 Tf_Cf
rename pvalues3 Tfsr_Cfsr
rename pvalues4 C_NC
rename pvalues5 Csr_NC
rename pvalues6 R_NC

drop R_NC

gen rownames = ""
replace rownames = "\midrule Total Students in Senior Year" if _n==1
replace rownames = "Total Female Students in Senior Year" if _n==2
replace rownames = "Average Age of Senior Year Students" if _n==3
replace rownames = "Morning Shift (1 = Yes, 0 = No)" if _n==4
replace rownames = "Afternoon Shift (1 = Yes, 0 = No)" if _n==5
replace rownames = "Full-Day School (1 = Yes, 0 = No)" if _n==6
replace rownames = "Public School (1 = Yes, 0 = No)" if _n==7
replace rownames = "Total Number of Labs" if _n==8
replace rownames = "Total Number of Libraries" if _n==9
replace rownames = "Total Number of Gyms" if _n==10
replace rownames = "Total Secondary Enrollment" if _n==11
replace rownames = "Teaches a Foreign Language (1 = Yes, 0 = No)" if _n==12
replace rownames = "Use of Educational Software (1 = Yes, 0 = No)" if _n==13
replace rownames = "Share of Computers per Student" if _n==14
replace rownames = "Share of Female Teachers" if _n==15
replace rownames = "Average Number of Teachers w$/$ Pedagogy Degree" if _n==16
replace rownames = "Average Magisterial Career Level of Teachers" if _n==17
replace rownames = "Average Teaching Hours of Teachers" if _n==18

replace rownames = "\midrule Number of Schools in Group 1" if _n==19
replace rownames = "Number of Schools in Group 2" if _n==20

order rownames

local vars T_C Tf_Cf Tfsr_Cfsr C_NC Csr_NC 

foreach var of local vars {
    gen `var'_s = string(`var', "%20.2fc")
	replace `var'_s = string(`var', "%20.0fc") if _n > 18
	drop `var'
	rename `var'_s `var'
}

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 4, before(1)
replace latex = "\begin{tabular}{lccccc}" in 1
replace latex = "\toprule Variables & p-value & p-value & p-value & p-value & p-value \\" in 2
replace latex = "& $ H_0: \mu_T = \mu_C $ & $ H_0: \mu_T^{*R} = \mu_C^{*R} $ & $ H_0: \mu_T^{*NR} = \mu_C^{*NR} $ & $ H_0: \mu_C^{R} = \mu_{NC}$ & $ H_0: \mu_C^{NR} = \mu_{NC}$ \\" in 3
replace latex = "& (1) & (2) & (3) & (4) & (5) \\" in 4

insobs 2, after(24)
replace latex = "\bottomrule & & & & & \\" in 25
replace latex = "\end{tabular}" in 26

cap file close myfile
file open myfile using "$results/Tables/school_characteristics_2.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

********************************************************************************
* Appendix Table B.4: Distribution of Replacement Schools Across Strata
********************************************************************************
use "$inputs/censo_2014_F0.dta", replace

keep if sample == 1

keep cod_mod treatment strata_d compliant

rename treatment treatment_d

gen initial_sample = 1

gen non_compliants = compliant == 2

gen replacements = compliant == 3

gen control_non_com = treatment_d == 0 & compliant == 2
gen control_replace = treatment_d == 0 & compliant == 3
gen treatment_non_com = treatment_d == 1 & compliant == 2
gen treatment_replace = treatment_d == 1 & compliant == 3

collapse (sum) initial_sample non_compliants replacements control* treatment*, by(strata_d)

drop treatment_d

gen type = . 
replace type = 1 if non_compliants != 0 & replacements != 0 & non_compliants <= replacements
replace type = 2 if non_compliants != 0 & replacements != 0 & non_compliants > replacements
replace type = 4 if non_compliants == 0
replace type = 3 if type == .

gen count = 1

collapse (sum) count non_compliants replacements, by(type)

gen type_name = ""
replace type_name = "\midrule Fully replaced within same stratum" if type == 1
replace type_name = "Partially replaced within same stratum" if type == 2
replace type_name = "No within-stratum replacements" if type == 3
replace type_name = "No inaccessible schools" if type == 4

order type_name
drop type

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 3, before(1)
replace latex = "\begin{tabular}{lccc}" in 1
replace latex = "\toprule & Number of Strata & Number of Inaccessible Schools & Number of Replacements Available \\" in 2
replace latex = "& (1) & (2) & (3) \\" in 3

insobs 3, after(7)
replace latex = "\midrule Total & 40 & 74 & 50 \\" in 8
replace latex = "\bottomrule & & & \\" in 9
replace latex = "\end{tabular}" in 10

cap file close myfile
file open myfile using "$results/Tables/replacements_strata.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile

